package com.flkin.modules.test.controller;

import com.flkin.common.base.Ret;
import com.flkin.modules.test.domain.Zoo;
import com.flkin.modules.test.service.ZooService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yanxiang.huang 2020-12-12 13:33:10
 */
@RestController
@RequestMapping("/api/zoo")
@Slf4j
public class ZooController {

    @Resource
    private ZooService zooService;

    @GetMapping
    @PreAuthorize("@sec.check('zooRead')")
    public ResponseEntity<Object> getAll() {
        return Ret.ok(zooService.getAll());
    }

    @GetMapping("/{id}")
    @PreAuthorize("@sec.check('zooRead')")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return Ret.ok(zooService.findById(id));
    }

    @PostMapping
    @PreAuthorize("@sec.check('zooCreate')")
    public ResponseEntity<Object> create(@RequestBody Zoo resource) {
        return Ret.ok(zooService.create(resource));
    }

    @PutMapping
    @PreAuthorize("@sec.check('zooUpdate')")
    public ResponseEntity<Object> update(@RequestBody Zoo resource) {
        zooService.update(resource);
        return Ret.ok();
    }

    @DeleteMapping
    @PreAuthorize("@sec.check('zooDelete')")
    public ResponseEntity<Object> delete(@RequestBody List<Long> ids) {
        zooService.deleteAll(ids);
        return Ret.ok();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@sec.check('zooDelete')")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        zooService.deleteById(id);
        return Ret.ok();
    }

}
